package com.jg.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jg.comm.responce.PageResult;
import com.jg.dingtalk.DingTalkService;
import com.jg.model.dto.InventoryQuery;
import com.jg.model.entity.Inventory;
import com.jg.mapper.InventoryMapper;
import com.jg.model.vo.InventoryVo;
import com.jg.service.InventoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 库存表 服务实现类
 * </p>
 *
 * @author 86130
 * @since 2025-09-19 11:21
 */
@Service
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory> implements InventoryService {
    @Autowired
    private InventoryMapper inventoryMapper;
    @Autowired
    private DingTalkService dingTalkService;

    @Override
    public PageResult<InventoryVo> pageList(InventoryQuery inventoryQuery) {
        Page<InventoryVo> page = Page.of(inventoryQuery.getPageNum(), inventoryQuery.getPageSize());
        Page<InventoryVo> inventoryPage = inventoryMapper.pageList(page, inventoryQuery);
        return PageResult.of(inventoryPage);
    }

    @Override
    public List<InventoryVo> getLowWarningInventoryList() {
        List<InventoryVo> inventoryList = inventoryMapper.getLowWarningInventoryList();
        return inventoryList;
    }

    @Override
    public void checkInventoryAndSendWarning() {
        //获取低于安全库存
        List<InventoryVo> inventoryList = inventoryMapper.getLowWarningInventoryList();
        if (!inventoryList.isEmpty()) {
            // 发送dd
            dingTalkService.sendInventoryWarning(inventoryList);
            //todo 发送websocket 消息到页面
            //todo 发送短信
            //日志打印
            inventoryList.forEach(inventoryVo -> {
                System.out.printf("商品: %s, 当前库存: %d, 阈值: %d%n",
                        inventoryVo.getProductName(), inventoryVo.getQuantity(), inventoryVo.getSafeQuantity());
            });
        } else {
            System.out.println("库存检查完成: 所有商品库存正常");
        }
    }
}
